#include <iostream>
#include "systemUI.h"
#include "ticket.h"
#include "time.h"
using std::cout;
using std::cin;
using std::endl;
using std::string;

void systemUI::mainMenu(){
    tManager.loadTicketsFromFile();
    oManager.loadOrdersFromFile();
    while(true){
        char choice;
        string password;
        cout << "=== 首页 ===" << endl;
        cout << "1.购票界面\n2.票务管理(仅限管理员)\n3.退出\n请选择:";
        cin >> choice;
        switch(choice){
        case '1':
            bootMenu();
            break;
        case '2':
            cout << "请输入密码: " ;
            cin >> password;
            if(password == "admin123"){
                cout << "密码正确!" << endl;
                manageMenu();
            }else{
                cout << "密码错误!" << endl;
            }
            break;
        case '3':
            tManager.saveTickets2File();
            oManager.saveOrders2File(); 
            return;
        default:
            cout << "无效选项!" << endl;
        }
    }
}

void systemUI::bootMenu(){
    while(true){
        char choice;
        cout << "=== 购票界面 ===" << endl;
        cout << "1.查看所有车票\n2.查询\n3.订票\n4.查看订单\n5.返回上级\n请选择:";
        cin >> choice;
        switch(choice){
        case '1':
            tManager.showAllTickets();
            break;
        case '2':
            searchMenu();
            break;
        case '3':
            tManager.bookTicket(oManager);
            break;
        case '4':
            oManager.showAllOrders();
            break;
        case '5':
            return;
        default:
            cout << "无效选项!" << endl;
        }
    }
}

void systemUI::manageMenu(){
    while(true){
        char choice;
        cout << "=== 管理界面 ===" << endl;
        cout << "1.查看所有车票\n2.查询车票\n3.增加车次\n4.删除车次\n5.调整车次/车票\n6.退出管理界面\n请选择:";
        cin >> choice;
        switch(choice){
        case '1':
            tManager.showAllTickets();
            break;
        case '2':
            searchMenu();
            break;
        case '3':
            tManager.addTicket();
            break;
        case '4':
            tManager.removeTicket();
            break;
        case '5':
            tManager.modifyTicket();
            break;
        case '6':
            return ;
        default:
            cout << "无效选项!" << endl;
        }
    }
}

void systemUI::searchMenu(){
    while(true){
        char choice;
        string input;
        TicketList dprList;
        TicketList arvList;
        
        cout << "=== 查询界面 ===" << endl;
        cout << "1.按车次\n2.按始发地\n3.按目的地\n4.返回上级\n请选择:";
        cin >> choice;
        switch(choice){
        case '1':
            cout << "请输入车次: ";
            cin >> input;
            if(auto train = tManager.codeSearch(input)){
                train->showTicketInline();
                cout << "是否订票? (y/n): ";
                cin >> choice;
                if(choice == 'y'){
                    tManager.bookTicket(oManager);
                }else if (choice == 'n'){
                }else{
                    cout << "无效选项!" << endl;
                }
                
            }else{
                cout << "车次不存在!" << endl;
            }
            break;
        case '2':
            cout << "请输入始发地: ";
            cin >> input;
            dprList = tManager.dprSearch(input);
            if(!dprList.isEmpty()){
                dprList.showAllTickets();
            }else{
                cout << "不存在始发地为" << input << "的车次!" << endl;
            }
            break;
        case '3':
            cout << "请输入目的地: ";
            cin >> input;
            arvList = tManager.arvSearch(input);
            if(!arvList.isEmpty()){
                arvList.showAllTickets();
            }else{
                cout << "不存在目的地为" << input << "的车次!" << endl;
            }
            break;
        case '4':
            return;
        default:
            cout << "无效选项!" << endl;
        }
    }
}
